/*控制脚本*/
//磁道序列
var indexArr = new Array();
//获取折线图对象
var lineObj ;

//对于产生序列按钮的控制
$("#btnCreateArray").on('click', function(){
	var number = $("#inNumber").val();
	var inHead = $("#inHead").val();
	if(isInteger(number) && number>0 && isInteger(inHead) && inHead>=0){
		$("#tabArray").show(500);
		$("#divAlgorithm").show(500);
		indexArr = createIndexsArray(number);
		showIndexArr(indexArr);
		lineObj = getSingel(getLineOption);//获取折线图对象
		lineObj.time = createTime(number);
		console.log(lineObj.time);
	}else{
		alert("输入有误，请重新输入！");
	}
});

//对于重新模拟的按钮的控制
$("#btnReload").on('click', function(){
	window.location.reload();
});

//显示、隐藏磁道序列
$("#btnDisplay").on('click', function(){
	var $pane = $("#tabArray");
	var pane = $pane.get(0);
	if(pane.style.display == 'none'){
		$pane.show(500);
		$(this).html("隐藏磁道序列");
	}else{
		$pane.hide(500);
		$(this).html("显示磁道序列");
	}
});

//对于算法选择的按钮的控制
$(".btnAlg").on('click', function(){
	var id = $(this).attr('id');
	//设置按钮失效
	if(id == 'btnAll'){
		$(".btnAlg").prop("disabled", true);
	}else{
		$(this).prop("disabled", true);//设置为不可点击
	}
	//console.log(id);
	resultProxy(id);
});

//结果显示代理
var results = {
	"btnFCFS": {
		id: "showFCFS",
		init: false,
		method: function(){
			var head = $("#inHead").val();
			var result = FCFS(indexArr, head);
			this.init = true;
			lineObj.index.push("FCFS");
			var item = {
				name:'FCFS',
	            type:'line',
	            // stack: '磁道',
	            data:result.arrProc
			}
			lineObj.members.push(item);
			initLineChart(lineObj);
			displayResult(result, this.id);
		}
	},
	"btnSSTF": {
		id: "showSSTF",
		init: false,
		method: function(){
			var head = $("#inHead").val();
			var result = SSTF(indexArr, head);
			this.init = true;
			lineObj.index.push("SSTF");
			var item = {
				name:'SSTF',
	            type:'line',
	            // stack: '磁道',
	            data:result.arrProc
			}
			lineObj.members.push(item);
			initLineChart(lineObj);
			displayResult(result, this.id);
		}
	},
	"btnSCAN": {
		id: "showSCAN",
		init: false,
		method: function(){
			var head = $("#inHead").val();
			var result = SCAN(indexArr, head);
			this.init = true;
			lineObj.index.push("SCAN");
			var item = {
				name:'SCAN',
	            type:'line',
	            // stack: '磁道',
	            data:result.arrProc
			}
			lineObj.members.push(item);
			initLineChart(lineObj);
			displayResult(result, this.id);
		}
	},
	"btnC-SCAN": {
		id: "showC-SCAN",
		init: false,
		method: function(){
			var head = $("#inHead").val();
			var result = CSCAN(indexArr, head);
			this.init = true;
			lineObj.index.push("C-SCAN");
			var item = {
				name:'C-SCAN',
	            type:'line',
	            // stack: '磁道',
	            data:result.arrProc
			}
			lineObj.members.push(item);
			initLineChart(lineObj);
			displayResult(result, this.id);
		}
	},
	"btnAll": {
		id: "showAll",
		method: function(){
			var head = $("#inHead").val();
			if(!results["btnFCFS"].init){
				results["btnFCFS"].method();
			}
			if(!results["btnSSTF"].init){
				results["btnSSTF"].method();
			}
			if(!results["btnSCAN"].init){
				results["btnSCAN"].method();
			}
			if(!results["btnC-SCAN"].init){
				results["btnC-SCAN"].method();
			};
		}
	}
}

var resultProxy = function(method){
	var obj = results[method];
	if(obj.id == "showAll"){
		$(".tabResult").show(500);
	}else{
		$("#"+obj.id).show(500);
	}
	obj.method();
}

//判断是否为整数
function isInteger(obj){
	return Math.round(obj) == obj;
}

//加载折线图
function initLineChart(obj){
	/*
		这里面要获取记载折线图的所需参数
	*/
	var chart = echarts.init(document.getElementById("line"));
	chart.setOption(getOption(obj));
}
